﻿namespace KuiHuaBaoDian.Services.UGUI {

    /// <summary>
    /// 实例化UI的可选项
    /// </summary>
    public static class UIOptions {

        public const string THEME_DEFAULT = "default";

        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="theme">主题</param>
        /// <param name="isRequired">是否是必要的，如果值为<code>TRUE</code>则之后的UI请求需要等级其完成</param>
        /// <param name="cacheGroup">缓存组名称，如果值为非空有长度字符串，则这个UI资产会加入持续存在于内存中</param>
        public static IUIOptions Instantiate(string theme, bool isRequired = false, string cacheGroup = null) {
            var instance = KHBD.TypeMap.Instantiate<IUIOptions>();
            if (string.IsNullOrWhiteSpace(theme)) {
                theme = THEME_DEFAULT;
            }
            (instance as IUIOptions.IInitializer).Initialize(theme, isRequired, cacheGroup);
            return instance;
        }

        /// <summary>
        /// 实例化
        /// <param name="isRequired">是否是必要的，如果值为<code>TRUE</code>则之后的UI请求需要等级其完成</param>
        /// <param name="cacheGroup">缓存组名称，如果值为非空有长度字符串，则这个UI资产会加入持续存在于内存中</param>
        /// </summary>
        public static IUIOptions Instantiate(bool isRequired = false, string cacheGroup = null) {
            var instance = KHBD.TypeMap.Instantiate<IUIOptions>();
            (instance as IUIOptions.IInitializer).Initialize(THEME_DEFAULT, isRequired, cacheGroup);
            return instance;
        }
    }

    /// <summary>
    /// 实例化UI的可选项
    /// </summary>
    public interface IUIOptions {

        /// <summary>
        /// 初始化写接口
        /// </summary>
        public interface IInitializer {

            /// <summary>
            /// 初始化
            /// </summary>
            /// <param name="theme">主题</param>
            /// <param name="isRequired">是否是必要的，如果值为<code>TRUE</code>则之后的UI请求需要等级其完成</param>
            /// <param name="cacheGroup">缓存组名称，如果值为非空有长度字符串，则这个UI资产会加入持续存在于内存中</param>
            void Initialize(string theme, bool isRequired = false, string cacheGroup = null);
        }

        /// <summary>
        /// 是否是必要的，如果值为<code>TRUE</code>则之后的UI请求需要等其完成
        /// </summary>
        bool IsRequired { get; }

        /// <summary>
        /// 主题
        /// </summary>
        string Theme { get; }

        /// <summary>
        /// 缓存组名称，如果值为非空有长度字符串，则这个UI资产会加入持续存在于内存中
        /// TODO: 需要实现的功能
        /// </summary>
        string CacheGroup { get; }
    }

    [Implement(typeof(IUIOptions))]
    internal sealed class KHBD_UIOptions : IUIOptions, IUIOptions.IInitializer {

        public string Theme { get; private set; }

        public bool IsRequired { get; private set; }

        public string CacheGroup { get; private set; }

        void IUIOptions.IInitializer.Initialize(string theme, bool isRequired, string cacheGroup) {
            Theme = theme;
            IsRequired = isRequired;
            CacheGroup = cacheGroup;
        }
    }
}
